package com.caldremch.androidfoundation.http.request;

import com.caldremch.androidfoundation.http.XHttp;
import com.caldremch.androidfoundation.http.convert.MyGsonConverterFactory;
import com.caldremch.androidfoundation.http.header.HttpHeaders;
import com.caldremch.androidfoundation.http.interceptor.HeadersInterceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * 基础请求器, simple
 *
 * @param <R> detail type
 */
public abstract class BaseRequest<R extends BaseRequest> {

    protected Retrofit mRetrofit; //Retrofit对象

    protected List<Interceptor> mInterceptors = new ArrayList<>(); //局部请求的拦截器

    protected List<Interceptor> mNetworkInterceptors = new ArrayList<>(); //局部请求的网络拦截器

    protected HttpHeaders mHeaders = new HttpHeaders(); //请求头
    protected String mBaseUrl; //基础域名
    protected Object mTag; //请求标签 , 用于取消网络请求
    protected long mReadTimeOut; //读取超时时间
    protected long mWriteTimeOut; //写入超时时间
    protected long mConnectTimeOut; //连接超时时间
    protected boolean mIsHttpCache; //暂不支持缓存

    public R baseUrl(String baseUrl) {
        if (baseUrl != null) {
            this.mBaseUrl = baseUrl;
        }
        return (R) this;
    }

    /**
     * 添加请求头
     *
     * @param headerKey   key
     * @param headerValue value
     * @return
     */
    public R addHeader(String headerKey, String headerValue) {
        this.mHeaders.put(headerKey, headerValue);
        return (R) this;
    }

    /**
     * 添加请求头
     *
     * @param headers header map
     * @return
     */
    public R addHeaders(Map<String, String> headers) {
        this.mHeaders.put(headers);
        return (R) this;
    }

    /**
     * 设置请求头
     *
     * @param headers HttpHeaders object info
     * @return
     */
    public R headers(HttpHeaders headers) {
        if (headers != null) {
            this.mHeaders = headers;
        }
        return (R) this;
    }

    /**
     * 设置请求标签
     *
     * @param tag request tag
     * @return
     */
    public R tag(Object tag) {
        this.mTag = tag;
        return (R) this;
    }


    /**
     * 局部设置拦截器
     *
     * @param interceptor interceptor
     * @return
     */
    public R interceptor(Interceptor interceptor) {
        if (interceptor != null) {
            this.mInterceptors.add(interceptor);
        }
        return (R) this;
    }

    /**
     * 生成局部配置
     */
    protected void newConfig() {

        OkHttpClient.Builder newBuilder = XHttp.getOkHttpClient().newBuilder();

        if (!mInterceptors.isEmpty()) {
            for (Interceptor interceptor : mInterceptors) {
                newBuilder.addInterceptor(interceptor);
            }
        }

        if (mHeaders.headersMap.size() > 0) {
            newBuilder.addInterceptor(new HeadersInterceptor(mHeaders.headersMap));
        }

        if (mBaseUrl != null) {
            Retrofit.Builder newRetrofitBuilder = new Retrofit.Builder();
            newRetrofitBuilder.baseUrl(mBaseUrl);
            newRetrofitBuilder.addConverterFactory(MyGsonConverterFactory.create());
            newRetrofitBuilder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
            newRetrofitBuilder.client(newBuilder.build());
            mRetrofit = newRetrofitBuilder.build();
        } else {
            XHttp.getRetrofitBuilder().client(newBuilder.build());
            mRetrofit = XHttp.getRetrofitBuilder().build();
        }
    }
}
